package com.example.im.controller.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.im.R;
import com.example.im.model.bean.UserInfo;

import java.util.ArrayList;
import java.util.List;

public class GroupDetailAdapter extends BaseAdapter {
    private Context mContext;
    private boolean mIsCanModify;//是否允许添加和删除群成员
    private boolean mIsDeleteModel;//删除模式 true：表示可以删除 false：表示不可以删除
    private List<UserInfo> mUsers = new ArrayList<>();
    private OnGroupDetailListener mOnGroupDetailListener;
    public GroupDetailAdapter(Context context,boolean isCanModify,OnGroupDetailListener onGroupDetailListener){
        mContext = context;
        mIsCanModify = isCanModify;
        mOnGroupDetailListener = onGroupDetailListener;
    }

    public boolean ismIsDeleteModel() {
        return mIsDeleteModel;
    }

    public void setmIsDeleteModel(boolean mIsDeleteModel) {
        this.mIsDeleteModel = mIsDeleteModel;
    }

    //刷新数据
    public void refresh(List<UserInfo> users){
        if (users != null && users.size()>=0){
            mUsers.clear();

            //添加加号和减号
            initUsers();

            mUsers.addAll(0,users);
        }
        //刷新页面
        notifyDataSetChanged();
    }

    private void initUsers() {
        UserInfo add = new UserInfo("add");
        UserInfo delete = new UserInfo("delete");
        mUsers.add(delete);
        mUsers.add(0,add);
    }

    @Override
    public int getCount() {
        return mUsers == null ? 0:mUsers.size();
    }

    @Override
    public Object getItem(int position) {
        return mUsers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        //获取或创建viewholder
        ViewHolder viewHolder = null;

        if (view == null){
            viewHolder = new ViewHolder();

            view = View.inflate(mContext, R.layout.item_groupdetail,null);

            viewHolder.iv_group_detail_delete = view.findViewById(R.id.iv_group_detail_delete);
            viewHolder.iv_group_detail_photo = view.findViewById(R.id.iv_group_detail_photo);
            viewHolder.tv_group_detail_name = view.findViewById(R.id.tv_group_detail_name);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }

        //获取当前item数据

        UserInfo userInfo = mUsers.get(position);
        if (mIsCanModify){//群主或开放了群权限
            //布局的处理
            if (position == getCount() -1){//减号的处理
                if (mIsDeleteModel){
                    view.setVisibility(View.INVISIBLE);
                }else {
                    view.setVisibility(View.VISIBLE);

                    /**
                     * View.VISIBLE--->可见
                     * View.INVISIBLE--->不可见，但这个View仍然会占用在xml文件中所分配的布局空间，不重新layout
                     * View.GONE---->不可见，但这个View在ViewGroup中不保留位置，会重新layout，不再占用空间，那后面的view就会取代他的位置
                     */
                    viewHolder.iv_group_detail_photo.setImageResource(R.drawable.em_smiley_minus_btn_pressed);
                    viewHolder.iv_group_detail_delete.setVisibility(View.GONE);
                    viewHolder.tv_group_detail_name.setVisibility(View.INVISIBLE);
                }

            }else if (position == getCount() -2){//加号的处理

                //删除模式判断
                if (mIsDeleteModel){
                    view.setVisibility(View.INVISIBLE);
                }else {
                    view.setVisibility(View.VISIBLE);
                    viewHolder.iv_group_detail_photo.setImageResource(R.drawable.em_smiley_add_btn_pressed);
                    viewHolder.iv_group_detail_delete.setVisibility(View.GONE);
                    viewHolder.tv_group_detail_name.setVisibility(View.INVISIBLE);
                }
            }else {//群成员
                view.setVisibility(View.VISIBLE);
                viewHolder.tv_group_detail_name.setVisibility(View.VISIBLE);

                viewHolder.tv_group_detail_name.setText(userInfo.getName());
                viewHolder.iv_group_detail_photo.setImageResource(R.drawable.em_default_avatar);
                if (mIsDeleteModel){
                    viewHolder.iv_group_detail_delete.setVisibility(View.VISIBLE);
                }else {
                    viewHolder.iv_group_detail_delete.setVisibility(View.GONE);
                }
            }
            //点击事件的处理
            if (position == getCount() -1){//减号
                viewHolder.iv_group_detail_photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!mIsDeleteModel){//如果不是删除模式
                            //设置为删除模式
                            mIsDeleteModel = true;
                            //刷新页面
                            notifyDataSetChanged();
                        }

                    }
                });
            }else if (position == getCount() -2){//加号
                viewHolder.iv_group_detail_photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mOnGroupDetailListener.onAddMembers();
                    }
                });
            }else {
                viewHolder.iv_group_detail_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mOnGroupDetailListener.onDeleteMember(userInfo);
                    }
                });
            }

        }else {//普通的群成员
            if (position == getCount() -1 || position == getCount() - 2){
                view.setVisibility(View.GONE);
            }else {
                view.setVisibility(View.VISIBLE);

                //名称
                viewHolder.tv_group_detail_name.setText(userInfo.getName());
                //头像
                viewHolder.iv_group_detail_photo.setImageResource(R.drawable.em_default_avatar);
                //删除
                viewHolder.iv_group_detail_delete.setVisibility(View.GONE);
            }
        }
        //显示数据

        //返回view
        return view;
    }
    private class ViewHolder{

        private ImageView iv_group_detail_photo;
        private ImageView iv_group_detail_delete;
        private TextView tv_group_detail_name;
    }

    public interface OnGroupDetailListener{
        //添加群成员方法
        void onAddMembers();

        //删除群成员方法
        void onDeleteMember(UserInfo user);
    }
}
